package com.cxs.studentManagerSystem.servlet;

import com.cxs.studentManagerSystem.auth.AuthUser;
import com.cxs.studentManagerSystem.base.BasePageBean;
import com.cxs.studentManagerSystem.base.BaseResult;
import com.cxs.studentManagerSystem.base.BaseServlet;
import com.cxs.studentManagerSystem.pojo.Student;
import com.cxs.studentManagerSystem.req.user.UserListReq;
import com.cxs.studentManagerSystem.req.user.UserLoginReq;
import com.cxs.studentManagerSystem.service.UserService;
import com.cxs.studentManagerSystem.service.impl.UserServiceImpl;
import com.cxs.studentManagerSystem.utils.BeanUtils;
import com.cxs.studentManagerSystem.utils.WebUtil;
import com.cxs.studentManagerSystem.vo.StudentVO;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Project: studentManagerSystem
 * @Author: cxs2014501@163.com
 * @Create: 2022/9/29 15:04
 * @Description:
 **/
@WebServlet(name = "userServlet", urlPatterns = "/user")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();


    private String login(HttpServletRequest request, HttpServletResponse response) {
        UserLoginReq loginReq = BeanUtils.requestParamAsBean(request, UserLoginReq.class, true);
        if (null == loginReq) {
            request.setAttribute("errMsg", "参数不能为空");
            return WebUtil.forwordUrl("/pages/login.jsp");
        }
        if (null == loginReq.getUsername() || loginReq.getUsername().equals("")) {
            request.setAttribute("errMsg", "用户名不能为空");
            return WebUtil.forwordUrl("/pages/login.jsp");
        }
        if (null == loginReq.getPassword() || loginReq.getPassword().equals("")) {
            request.setAttribute("errMsg", "密码不能为空");
            return WebUtil.forwordUrl("/pages/login.jsp");
        }
        AuthUser loginUser = userService.userLogin(loginReq);
        if (null == loginUser) {
            request.setAttribute("errMsg", "用户名或密码错误,请重新操作");
            return WebUtil.forwordUrl("/pages/login.jsp");
        }
        HttpSession session = request.getSession();
        session.setAttribute("user", loginUser);
        return WebUtil.redirectUrl(request, "/pages/index.jsp");
    }

    /**
     * 获取用户信息
     * @param request
     * @param response
     * @return
     */
    private BaseResult<AuthUser> getUserInfo(HttpServletRequest request, HttpServletResponse response) {
        BaseResult<AuthUser> result = BaseResult.ok();
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (null == user) {
            result.setCode(401);
            response.setStatus(401);
            result.setMsg("用户登录信息已过期");
            return result;
        } else {
            AuthUser loginUser = (AuthUser) user;
            result.setData(loginUser);
            return result;
        }
    }

    /**
     * 退出系统
     * @param request
     * @param response
     * @return
     */
    private String logout(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        session.invalidate();
        return WebUtil.redirectUrl(request, "/pages/login.jsp");
    }

    /**
     * 分页获取用户列表
     * @param request
     * @param response
     * @return
     */
    private String userListPage(HttpServletRequest request, HttpServletResponse response) {
        UserListReq listReq = BeanUtils.requestParamAsBean(request, UserListReq.class, true);
        if (null == listReq) {
            listReq = new UserListReq();
        }
        if (null == listReq.getPageNum() || listReq.getPageNum() <= 0) {
            listReq.setPageNum(1);
        }
        if (null == listReq.getPageSize() || listReq.getPageSize() <= 0) {
            listReq.setPageSize(10);
        }
        BasePageBean<StudentVO> pageBean = userService.userListPage(listReq);
        request.setAttribute("page", pageBean);
        return WebUtil.forwordUrl("/pages/user/userList.jsp");
    }

    /**
     * 修改
     * @param request
     * @param response
     * @return
     */
    private String updateUserInfo(HttpServletRequest request, HttpServletResponse response) {
        Student student = BeanUtils.requestParamAsBean(request, Student.class, true);
        if (null == student) {
            request.setAttribute("errMsg", "入参错误");
            return WebUtil.forwordUrl("/pages/user/userInfo.jsp");
        }
        if (null == student.getId()) {
            request.setAttribute("errMsg", "用户id不能为空");
            return WebUtil.forwordUrl("/pages/user/userInfo.jsp");
        }
        Integer result = userService.updateSelectById(student);
        return WebUtil.forwordUrl("/pages/user/userList.jsp");
    }

    /**
     * 获取用户信息
     * @param request
     * @param response
     * @return
     */
    private BaseResult<Student> getUserInfoById(HttpServletRequest request, HttpServletResponse response) {
        BaseResult<Student> result = BaseResult.ok();
        String id = request.getParameter("id");
        Student student = null;
        if (id == null || id.equals("")) {
            result.setCode(400);
            result.setMsg("id不能为空!");
            return result;
        } else {
            try {
                student = userService.getUserInfoById(Integer.parseInt(id));
            } catch (NumberFormatException e) {
                result.setCode(400);
                result.setMsg("非法的用户id!");
                return result;
            }
            result.setData(student);
        }
        return result;
    }
}
